package yongxin.entity.process;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.SequenceGenerator;

/**   
 * @Title: Entity
 * @Description: 工序汇报流程
 * @author zhangdaihao
 * @date 2015-02-05 14:03:49
 * @version V1.0   
 *
 */
@SuppressWarnings("serial")
public class ProcessJsonEntity implements java.io.Serializable {
	
	/*
	 * 
	 * jsondata={
		title:"aaa",
		nodes:{
			demo_node_1:{name:"任务单<br>(YJ1503001)",left:67,top:71,type:"start",width:150,height:24},
			demo_node_2:{name:"轧机<br>(YJ1503001)",left:269,top:71,type:"node",width:150,height:24},
			demo_node_5:{name:"切边机<br>(YJ1503001)",left:480,top:71,type:"node",width:150,height:24},
			demo_node_6:{name:"切边机<br>(YJ1503001)",left:680,top:71,type:"fork",width:150,height:24},
			demo_node_7:{name:"切边机<br>(YJ1503001-A)",left:680,top:171,type:"node",width:150,height:24},
			demo_node_8:{name:"切边机<br>(YJ1503001-A)",left:480,top:171,type:"node",width:150,height:24}
		},
		lines:{
			demo_line_3:{type:"sl",from:"demo_node_1",to:"demo_node_2",name:"",marked:false},
			demo_line_6:{type:"sl",from:"demo_node_2",to:"demo_node_5",name:"",marked:false},
			demo_line_7:{type:"sl",from:"demo_node_5",to:"demo_node_6",name:"",marked:false},
			demo_line_8:{type:"sl",from:"demo_node_6",to:"demo_node_7",name:"",marked:false},
			demo_line_9:{type:"sl",from:"demo_node_7",to:"demo_node_8",name:"",marked:false}
		},
		areas:{
			demo_area_8: {name:"area_8",left:35,top:39,color:"red",width:472,height:114}
		}
	};
	 * */
	/**title*/
	private java.lang.String title;
	/**nodes*/
	private Map<String,ProcessNodeJsonEntity> nodes=new HashMap<String, ProcessNodeJsonEntity>();
	/**lines*/
	private Map<String,ProcessLineJsonEntity> lines=new HashMap<String, ProcessLineJsonEntity>();
	
	/**areas*/
	private Map<String,ProcessAreaJsonEntity> areas=new HashMap<String, ProcessAreaJsonEntity>();
	
	ProcessNodeJsonEntity lastNode=new ProcessNodeJsonEntity();
	private static java.lang.Integer spacing=50;
	private static java.lang.Integer area_spacing=150;
	private int direct=0;
	private Map<String,ProcessNodeJsonEntity> group=new HashMap<String,ProcessNodeJsonEntity>();
	private Map<String,Integer> count=new HashMap<String,Integer>();
	private String[] colors=new String[]{"red","blue","green","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow","yellow"};
	
	public void addNode(ProcessNodeJsonEntity node){
		
		System.out.println(node.getFbcph()+"--"+node.getName());
		int left=0;
		int top=0;
		int size=0;
		
		
		if(group.containsKey(node.getFbcph())){  //如果存在流程，则取保存的最后一个，如果不存在则取默认顺序的最后一个
			lastNode=group.get(node.getFbcph());//本流程最后节点
			
			size=count.get(node.getFbcph());  //流程节点个数
			if(size%6==0){
				//direct=direct==0?1:0;  //反向
			}
			
			count.put(node.getFbcph(), count.get(node.getFbcph())+1);//计数+1
		}		
		else{

			count.put(node.getFbcph(), 1);
			//direct=0;
		}
	
		
		if(lastNode.getLeft()==0){//初始位置
			left=67;
			top=24;
			
		}
		else if(size%6!=0){  //非边界
			if(lastNode.getDirect()==0){ //向右
				left=lastNode.getLeft()+node.getWidth()+ProcessJsonEntity.spacing;
				top=lastNode.getTop();
			}
			else{  //向左
				left=lastNode.getLeft()-node.getWidth()-ProcessJsonEntity.spacing;
				top=lastNode.getTop();
				node.setDirect(1);
			}
			
		}
		else{   //边界区域
			left=lastNode.getLeft();
			top=lastNode.getTop()+node.getHeight()+ProcessJsonEntity.spacing;
			node.setDirect(lastNode.getDirect()==0?1:0);//反向
		}
		
		
		if(!group.containsKey(node.getFbcph())){  //分支节点
			
			
			if(group.size()>0 ){ // 分支增加间隔
				left=67;
				top=240*group.size()+ProcessJsonEntity.spacing;
				
				node.setType("fork");
				node.setSplit(true);   //标记上个节点为分支节点
				
				//lastNode.setSplit(true);  
				if(lastNode.getProcess().equals("over")){
					lastNode.setType("end");
				}
				else{
					lastNode.setType("fork");
				}
				
			}
			
			
		//	direct=0;
			node.setDirect(0);//反向
			
			ProcessAreaJsonEntity area=new ProcessAreaJsonEntity();//新起始区域
			area.setName(node.getFbcph());
			area.setColor(colors[group.size()]);
			area.setTop(top-20);
			area.setHeight(ProcessJsonEntity.spacing+node.getWidth());
			areas.put(node.getFbcph(), area);
			
			
			
		}
		else{  //正常流程
			ProcessAreaJsonEntity area=areas.get(node.getFbcph());
			
			//int height= size%6==0 ? size/6*node.getHeight()+size/6*ProcessJsonEntity.spacing : (size/6+1)*node.getHeight()+ProcessJsonEntity.spacing;
			int height=  (size/6+1)*(node.getHeight()+ProcessJsonEntity.spacing);
		//	area.setHeight(height);
			
			
			if(node.getProcess().equals("over")){
				node.setType("end");
			}
			
			 
		}
		
		
		
		node.setLeft(left);
		node.setTop(top);
		node.setId("node_"+(nodes.size()+1));
		nodes.put("node_"+(nodes.size()+1), node);
		
		if(lastNode.getLeft()>0 && !node.getSplit()){  //分支无连线
			ProcessLineJsonEntity line=new ProcessLineJsonEntity();
			line.setFrom(lastNode.getId());
			line.setTo(node.getId());
			line.setType("sl");
			line.setMarked(false);
			
			lines.put("line_"+nodes.size(), line);
		}
		
		
		
		lastNode=node;

		
		group.put(node.getFbcph(),node);
		
		
	}
	
	
	
	
	
	
	public java.lang.String getTitle() {
		return title;
	}
	public void setTitle(java.lang.String title) {
		this.title = title;
	}
	public Map<String, ProcessNodeJsonEntity> getNodes() {
		return nodes;
	}
	public void setNotes(Map<String, ProcessNodeJsonEntity> nodes) {
		this.nodes = nodes;
	}
	public Map<String, ProcessLineJsonEntity> getLines() {
		return lines;
	}
	public void setLines(Map<String, ProcessLineJsonEntity> lines) {
		this.lines = lines;
	}
	
	public Map<String, ProcessAreaJsonEntity> getAreas() {
		return areas;
	}
	public void setAreas(Map<String, ProcessAreaJsonEntity> areas) {
		this.areas = areas;
	}
	
	
}
